package top.college.service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLRecoverableException;
import java.util.Scanner;

import javax.swing.JPasswordField;
import javax.swing.JTextField;

import top.college.dao.JDBC_ConnClose;
import top.college.dao.JDBC_SQL;
import top.college.model.ProfessorVO;

public class ProfessorCreate {
	public static ProfessorVO pv = new ProfessorVO();
	public static boolean check;
	
	public static boolean checkProfessor(JTextField ProfNoIn ) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.ProfessorSelectAllSQL);
			pstmt.setString( 1, ProfNoIn.getText());
			rs = pstmt.executeQuery();
			if ( rs.next() ) {
				check = true;
				return true;
			} else { check = false; return false; }
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		check = false;
		return false;
	}
	
	public static String checkProfessorDepart(String deptno) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.ProfessorDepartSQL);
			pstmt.setString(1, deptno);
			rs = pstmt.executeQuery();
			if ( rs.next() ) {
				return rs.getString(1);				
			} else { return ""; }
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return null;
	}
	
	public static boolean createProfessor(JTextField ProfNoIn, JTextField ProfNameIn, JPasswordField ProfPW1In
			, JTextField ProfTelIn, JTextField ProfOfficeIn, JTextField ProfAddrIn) {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = JDBC_ConnClose.openDataBase();
			pstmt = conn.prepareStatement(JDBC_SQL.ProfessorInsertSQL);
			pstmt.setString( 1, ProfNoIn.getText() );
			pstmt.setString( 2, ProfPW1In.getText() );
			pstmt.setString( 3, ProfNameIn.getText() );
			pstmt.setString( 4, (ProfNoIn.getText()).substring(0, 2));
			pstmt.setString( 5, ProfTelIn.getText() );
			pstmt.setString( 6, ProfAddrIn.getText() );
			pstmt.setString( 7, ProfOfficeIn.getText() );
			pstmt.executeUpdate();
			return true;
		} catch ( Exception e ) {
			e.printStackTrace();
		} finally {
			JDBC_ConnClose.closeDataBase(conn, pstmt, rs);
		}
		return false;
	}
}